//
//  UIView+CornerRadius.m
//  YSProject
//
//  Created by 昱胜 on 2021/12/22.
//

#import "UIView+CornerRadius.h"

@implementation UIView (CornerRadius)

- (void)clipAllCornerWithWidth:(CGFloat)width {
    self.layer.cornerRadius = width;
    self.layer.masksToBounds = YES;
}

- (UIView *)clipCornerWithView:(UIView *)originView
                   cornerWidth:(CGFloat)cornerWidth
                    cornerType:(UIRectCorner)cornerType
{
    [originView layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:originView.bounds
                                                   byRoundingCorners:cornerType
                                                         cornerRadii:CGSizeMake(cornerWidth, cornerWidth)];
    // 创建遮罩层
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = originView.bounds;
    maskLayer.path = maskPath.CGPath;   // 轨迹
    originView.layer.mask = maskLayer;

    return originView;
}

- (void)configGradientWithView:(UIView *)view
                    startPoint:(CGPoint)startPoint
                      endPoint:(CGPoint)endPoint
                    startColor:(UIColor *)startColor
                      endColor:(UIColor *)endColor {
    [view layoutIfNeeded];
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = view.bounds;
    // 渐变色颜色数组,可多个
    gradientLayer.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
    // 渐变的开始点 (不同的起始点可以实现不同位置的渐变,如图)
    gradientLayer.startPoint = startPoint; //(0, 0)
    // 渐变的结束点
    gradientLayer.endPoint = endPoint; //(1, 1)
    [view.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)configDottedLineWithView:(UIView *)view
                       lineColor:(UIColor *)lineColor
                       lineWidth:(CGFloat)lineWidth {
    CAShapeLayer *border = [CAShapeLayer layer];
    //虚线的颜色
    border.strokeColor = lineColor.CGColor;
    //填充的颜色
    border.fillColor = [UIColor clearColor].CGColor;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.bounds cornerRadius:5];
    //设置路径
    border.path = path.CGPath;
    border.frame = view.bounds;
    //虚线的宽度
    border.lineWidth = lineWidth;
    //设置线条的样式
    //    border.lineCap = @"square";
    //虚线的间隔
    border.lineDashPattern = @[@4, @2];
    [view.layer addSublayer:border];
}


// 创建一张渐变色图片
- (UIImage *)createImageSize:(CGSize)imageSize gradientColors:(NSArray *)colors percentage:(NSArray *)percents {
    
    NSAssert(percents.count <= 5, @"输入颜色数量过多，如果需求数量过大，请修改locations[]数组的个数");
    
    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    
    //    NSUInteger capacity = percents.count;
    //    CGFloat locations[capacity];
    CGFloat locations[5];
    for (int i = 0; i < percents.count; i++) {
        locations[i] = [percents[i] floatValue];
    }
    
    UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, locations);
    CGPoint start = CGPointMake(0.0, imageSize.height);
    CGPoint end = CGPointMake(imageSize.width, 0.0);
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

/**
 压缩图片
 */
- (NSData *)zipNSDataWithImage:(UIImage *)sourceImage
{
    //进行图像尺寸的压缩
    CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
    CGFloat width = imageSize.width;    //图片宽度
    CGFloat height = imageSize.height;  //图片高度
    //1.宽高大于1280(宽高比不按照2来算，按照1来算)
    if (width > 1280 || height>1280) {
        if (width > height) {
            CGFloat scale = height / width;
            width = 1280;
            height = width * scale;
        } else {
            CGFloat scale = width / height;
            height = 1280;
            width = height * scale;
        }
        //2.宽大于1280高小于1280
    } else if (width > 1280 || height < 1280){
        CGFloat scale = height / width;
        width = 1280;
        height = width * scale;
        //3.宽小于1280高大于1280
    } else if (width < 1280 || height > 1280){
        CGFloat scale = width / height;
        height = 1280;
        width = height * scale;
        //4.宽高都小于1280
    } else {
        
    }
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    [sourceImage drawInRect:CGRectMake(0,0,width,height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //进行图像的画面质量压缩
    NSData *data = UIImageJPEGRepresentation(newImage, 1.0);
    if (data.length > 100 * 1024) {
        if (data.length > 1024 * 1024) {//1M以及以上
            data = UIImageJPEGRepresentation(newImage, 0.5);
        } else if (data.length > 512 * 1024) {//0.5M-1M
            data = UIImageJPEGRepresentation(newImage, 0.6);
        } else if (data.length > 200 * 1024) {
            //0.25M-0.5M
            data = UIImageJPEGRepresentation(newImage, 0.7);
        }
    }
    return data;
}


@end
